package com.yycx.module.bus.provider.service.hy.impl;

import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.module.bus.client.entity.hy.BusHyorderSendinfo;
import com.yycx.module.bus.provider.mapper.hy.BusHyorderSendinfoMapper;
import com.yycx.module.bus.provider.service.hy.BusHyorderSendinfoService;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.query.CriteriaQuery;
import org.springframework.transaction.annotation.Propagation;
import com.yycx.common.mybatis.query.CriteriaSave;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发货信息接口实现类
 *
 * @author flyme
 * @date 2023-09-30
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusHyorderSendinfoServiceImpl extends BaseServiceImpl<BusHyorderSendinfoMapper, BusHyorderSendinfo> implements BusHyorderSendinfoService {

    @Autowired
    private HyOrderService orderService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusHyorderSendinfo bhs, EntityMap extra) {
        return ResultBody.ok();
    }

    @Override
    public ResultBody afterAdd(CriteriaSave cs, BusHyorderSendinfo busHyorderSendinfo, EntityMap extra) {
        String orderId = busHyorderSendinfo.getOrderId();
        String invoiceNo = busHyorderSendinfo.getInvoiceNo();
        if (FlymeUtils.isNotEmpty(invoiceNo)) {
            orderService.updateInvoiceState(orderId, CommonConstants.INT_2);
        }
        return super.afterAdd(cs, busHyorderSendinfo, extra);
    }

    @Override
    public ResultBody afterEdit(CriteriaUpdate cu, BusHyorderSendinfo busHyorderSendinfo, EntityMap extra) {
        String orderId = busHyorderSendinfo.getOrderId();
        String invoiceNo = busHyorderSendinfo.getInvoiceNo();
        if (FlymeUtils.isNotEmpty(invoiceNo)) {
            orderService.updateInvoiceState(orderId, CommonConstants.INT_2);
        }
        return super.afterEdit(cu, busHyorderSendinfo, extra);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusHyorderSendinfo> cq, BusHyorderSendinfo bhs, EntityMap requestMap) {
        cq.orderByDesc("bhs.createTime");
        return ResultBody.ok();
    }

    @Override
    public BusHyorderSendinfo findByOrderId(String orderId) {
        CriteriaQuery cq = new CriteriaQuery(BusHyorderSendinfo.class);
        cq.eq(true, "orderId", orderId);
        return getOne(cq, false);
    }

    @Override
    public BusHyorderSendinfo findByLastCustomId(Long saleManId, Long customId) {


        return null;
    }
}
